<!-- page heading start-->
<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-lg-10">
        <h2>员工列表</h2>
        <ol class="breadcrumb">
            <li>
                <a href="/User/index/index">主页</a>
            </li>
            <li class="active">
                <strong>员工管理</strong>
            </li>
        </ol>
    </div>
    <div class="col-lg-2">

    </div>
</div>
<!-- page heading end-->
<div class="fh-breadcrumb">
    <div class="row">
        <div class="col-lg-12" style="background-color: white">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                </div>
                <div class="ibox-content" style="min-height: 500px;">
                    <div class="row">
                        <div class="col-sm-3 b-r" style="border-right:1px solid #7a7676;">
                            <h3 class="m-t-none m-b" style="padding: 10px;border-bottom: 1px solid #65CEA7;">
                                <a href="/User/Tool/employee">所有部门</a>
                                <button class="btn btn-success" style="margin-left: 50px;" data-toggle="modal" data-target="#myModal" data-dept-id="0"
                                        data-dept-name="" data-dept-emp=""
                                        data-dept-parent="0" onclick="dept_show(this)">新增部门</button>
                            </h3>
                            <?php
                            array_walk($dept, 'dept_print');
                            $pref = 0;
                            function dept_print($dept_v,$key, $pref){?>
                                    <div class="row">
                                        <div class="col-sm-8"><span style="margin-left: <?php echo $pref . 'px';?>">
                                            <a href="<?php echo '/User/Tool/employee?dept_id=' . $dept_v['id'];?>"><?php echo $dept_v['name']?></a></span></div>
                                        <a class="glyphicon glyphicon-plus dropdown-toggle" data-toggle="modal" data-target="#myModal" data-dept-id="0"
                                           data-dept-name="" data-dept-emp=""
                                           data-dept-parent="<?php echo $dept_v['id'];?>" onclick="dept_show(this)"
                                            ></a>
                                        <a class="dropdown-toggle"  data-toggle="modal" data-target="#myModal" data-dept-id="<?php echo $dept_v['id'];?>"
                                            data-dept-name="<?php echo $dept_v['name'];?>" data-dept-emp="<?php echo $dept_v['emp_id'];?>"
                                           data-dept-parent="<?php echo $dept_v['parent_id'];?>" onclick="dept_show(this)"
                                        >
                                            <i class="fa fa-wrench"></i>
                                        </a>
                                        <a class="close-link" onclick="delete_dept(this)" data-dept-id="<?php echo $dept_v['id'];?>">
                                            <i class="fa fa-times"></i>
                                        </a>
                                        <a class="collapse-link" onclick="toggle_up(this);" data-dept-id="<?php echo $dept_v['id'];?>"
                                           data-dept-parent="<?php echo $dept_v['parent_id'];?>" data-dept-priority="<?php echo $dept_v['priority'];?>">
                                            <i class="fa fa-chevron-up"></i>
                                        </a>
                                        <a class="collapse-link" onclick="toggle_down(this);" data-dept-id="<?php echo $dept_v['id'];?>"
                                           data-dept-parent="<?php echo $dept_v['parent_id'];?>" data-dept-priority="<?php echo $dept_v['priority'];?>">
                                            <i class="fa fa-chevron-down"></i>
                                        </a>
                                    </div>
                            <?php
                            if($dept_v['son'] != ''){
                                $pref = $pref+10;
                                array_walk($dept_v['son'], 'dept_print', $pref);
                            }}?>
                        </div>
                        <div class="col-sm-8">
                            <header class="panel-heading">
                                <button class="btn btn-primary" data-toggle="modal" onclick="return show(this)" data-id=""
                                        data-phone="" data-name="" data-deptId="" data-position="">
                                    增加
                                </button>
                                <button class="btn btn-success" onclick="return download();">
                                    导出
                                </button>
                                <div class="input-group" style="width: 250px;float: right;margin-right: 20px;">
                                    <input type="text" class="form-control input-lg" id="sname" placeholder="输入姓名搜索" value="<?php echo I('get.name', '', '');?>">
                                    <span class="input-group-addon btn btn-primary" onclick="submit_name()">搜索</span>
                                </div>
                                <div class="input-group" style="width: 250px;float: right;margin-right: 20px;">
                                    <input type="text" id="sphone" class="form-control input-lg" placeholder="输入手机号码搜索" value="<?php echo I('get.phone', '', '');?>">
                                    <span class="input-group-addon btn btn-primary" onclick="submit_phone()">搜索</span>
                                </div>
                            </header>
                            <div class="panel-body">
                                <table class="table table-hover general-table table-bordered">
                                    <thead>
                                    <tr>
                                        <th width="15%">名称</th>
                                        <th width="20%">所属部门</th>
                                        <th width="20%">职务</th>
                                        <th width="15%">手机号码</th>
                                        <th>操作</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <?php foreach($list as $val){?>
                                    <tr>
                                        <td>
                                            <span style="word-break:break-all;word-wrap:break-word">
                                                <?php echo $val['name'];?>
                                            </span>
                                        </td>
                                        <td>
                                            <span style="word-break:break-all;word-wrap:break-word">
                                                <?php echo $val['dept'];?>
                                            </span>
                                        </td>
                                        <td>
                                            <span style="word-break:break-all;word-wrap:break-word">
                                                <?php echo $val['position'];?>
                                            </span>
                                        </td>
                                        <td>
                                            <span style="word-break:break-all;word-wrap:break-word">
                                                <?php echo $val['phone'];?>
                                            </span>
                                        </td>
                                        <td>
                                            <div>
                                                <button class="btn btn-primary" onclick="return show(this)" data-id="<?php echo $val['id'];?>"
                                                        data-phone="<?php echo $val['phone'];?>"
                                                        data-name="<?php echo $val['name'];?>" data-deptId="<?php echo $val['dept_id'];?>"
                                                        data-position="<?php echo $val['position'];?>"
                                                >
                                                    编辑
                                                </button>
                                                <a class="btn btn-danger" href="#"
                                                   onclick="<?php echo 'return delete_data(' . $val['id'] .')';?>">删除</a>
                                            </div>
                                        </td>
                                    </tr>
                                    <?php }?>

                                    </tbody>
                                </table>
                                <div class="pages">
                                    {$page}
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="myModal_employee" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-body">
                <input type="hidden" id="id" value="<?php $val=(!empty($dept)) ?
                        $dept['id'] : ''; echo $val;?>">
                <div class="row">
                    <label class="form-inline">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;名称:
                    <input type="text" class="form-control" style="width: 300px;" name="name" id="emp_name"></label>
                </div>
                <div class="row">
                    <label class="form-inline">&nbsp;&nbsp;&nbsp;手机号码:
                        <input type="text" class="form-control" style="width: 300px;" name="phone" id="phone"></label>
                </div>
                <div class="row">
                    <label class="form-inline">&nbsp;&nbsp;&nbsp;所属部门:
                        <select class="selectpicker" id="emp_dept_id" style="width: 300px;" data-live-search="true">
                            <option value="">--无--</option>
                            <?php
                            foreach($dept_all_list as $dept_value){
                                echo "<option value='" . $dept_value['id'] . "'>" . $dept_value['name'] . "</option>";
                            }
                            ?>
                        </select></label>
                </div>
                <div class="row">
                    <label class="form-inline">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;职务:
                        <input type="text" class="form-control" style="width: 300px;" name="position" id="position"></label>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                </button>
                <button type="button" class="btn btn-primary" onclick="update();">
                    提交更改
                </button>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-body">
                <input type="hidden" id="dept_id" value="">
                <input type="hidden" id="parent_id" value="">
                <div class="row">
                    <label class="form-inline">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;名称:
                        <input type="text" id="dept_name" class="form-control" style="width: 300px;" name="name" id="name"></label>
                </div>
                <div class="row">
                    <label class="form-inline">部门负责人:
                        <select class="selectpicker" id="emp_id" style="width: 300px;" data-live-search="true">
                            <option value="">--无--</option>
                            <?php
                            foreach($emp_all_list as $emp){
                                echo "<option value='" . $emp['id'] . "'>" . $emp['name'] . "</option>";
                            }
                            ?>
                        </select></label>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                </button>
                <button type="button" class="btn btn-primary" onclick="update_dept();">
                    提交更改
                </button>
            </div>
        </div>
    </div>
</div>
<link rel="stylesheet" href="/Public/css/bootstrap-select.min.css">
<script type="text/javascript" src="/Public/js/bootstrap-select.min.js"></script>
<script>
    function update() {
        var id = $("#id").val();
        var name = $("#emp_name").val();
        var phone = $("#phone").val();
        var dept_id = $("#emp_dept_id").val();
        var position = $("#position").val();
        $.ajax({
            data:{id:id,
                name:name,
                phone:phone,
                dept_id:dept_id,
                position: position
            },
            dataType:'json',
            url:'/User/Tool/employeeUpdate',
            type:'get',
            success:function(res){
                window.location.reload();
            }
        })
    }

    function show(e){
        $("#id").val($(e).attr("data-id"));
        $("#emp_name").val($(e).attr("data-name"));
        $("#phone").val($(e).attr("data-phone"));
        $("#position").val($(e).attr("data-position"));
        $('#emp_dept_id').selectpicker('val', $(e).attr("data-deptId"));
        $('#myModal_employee').modal('show');
    }
    function delete_data(id){
        $.ajax({
            data:{id:id},
            dataType:'json',
            url:'/User/Tool/employeeDelete',
            type:'get',
            success:function(res){
                window.location.reload();
            }
        });
        return false;
    }
    function submit_name(){
        var  name = $("#sname").val();
        window.location.href = "/User/Tool/employee?name=" + name;
    }
    function submit_phone(){
        var  name = $("#sphone").val();
        window.location.href = "/User/Tool/employee?phone=" + name;
    }
    function toggle_up(e) {
        var id = $(e).attr("data-dept-id");
        var parent_id = $(e).attr("data-dept-parent");
        var priority = $(e).attr("data-dept-priority");
        $.ajax({
            data:{
                id:id,
                parent_id:parent_id,
                priority:priority,
                type:1
            },
            dataType:'json',
            url:'/User/Tool/ajaxDeptPriority',
            type:'get',
            success:function(res){
                window.location.reload();
            }
        })
    }
    function toggle_down(e) {
        var id = $(e).attr("data-dept-id");
        var parent_id = $(e).attr("data-dept-parent");
        var priority = $(e).attr("data-dept-priority");
        $.ajax({
            data:{
                id:id,
                parent_id:parent_id,
                priority:priority,
                type:2
            },
            dataType:'json',
            url:'/User/Tool/ajaxDeptPriority',
            type:'get',
            success:function(res){
                window.location.reload();
            }
        })
    }
    function dept_show(e){
        $("#dept_id").val($(e).attr("data-dept-id"));
        $("#dept_name").val($(e).attr("data-dept-name"));
        $("#parent_id").val($(e).attr("data-dept-parent"));
        $('#emp_id').selectpicker('val', $(e).attr("data-dept-emp"));
        $('#myModal').modal('show');
    }
    function update_dept() {
        var dept_id = $("#dept_id").val();
        var parent_id = $("#parent_id").val();
        var name = $("#dept_name").val();
        var emp_id = $("#emp_id").val();
        $.ajax({
            data:{
                id:dept_id,
                name:name,
                parent_id:parent_id,
                emp_id:emp_id
            },
            dataType:'json',
            url:'/User/Tool/ajaxDeptUpdate',
            type:'get',
            success:function(res){
                window.location.reload();
            }
        });
    }
    function delete_dept(e) {
        var id = $(e).attr("data-dept-id");
        $.ajax({
            data:{
                id:id
            },
            dataType:'json',
            url:'/User/Tool/deptDelete',
            type:'get',
            success:function(res){
                window.location.reload();
            }
        });
    }
    function download() {
        window.location = '/User/Tool/exportEmployee';
    }
</script>